package com.warpcode.gpsloggerlite;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class ShowDetailsActivity extends Activity
{
	public static final int EDIT_ENTRY_REQUEST_CODE = 1;
	private final static int RESULT_FAILED = 2;
        
	LocationDatabase db;
	
	TextView tim,lat,lon,alt,spe;
        
	String id;
        
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show_details);
                
		Intent intent = getIntent();
                
		db = new LocationDatabase(this);
                
		tim = (TextView) findViewById(R.id.time);
		lat = (TextView) findViewById(R.id.latitude);
        lon = (TextView) findViewById(R.id.longitude);
        alt = (TextView) findViewById(R.id.altitude);
        spe = (TextView) findViewById(R.id.speed);
                                
        id  = intent.getStringExtra("id");
        tim.setText(intent.getStringExtra("time"));
        lat.setText(intent.getStringExtra("latitude"));
        lon.setText(intent.getStringExtra("longitude"));
        alt.setText(intent.getStringExtra("altitude"));
        spe.setText(intent.getStringExtra("speed"));        
	}
        
		
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		switch (item.getItemId()) 
		{
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
		}
        return super.onOptionsItemSelected(item);
	}
	
	public void deleteData(View view)
	{
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        
    	alertDialogBuilder.setTitle("Confirm Delete");
    	alertDialogBuilder.setMessage("Are you sure you want to delete the entry?")
        	.setCancelable(false)
        	.setPositiveButton("No",
        	new DialogInterface.OnClickListener()
        	{        
        		public void onClick(DialogInterface dialog, int id)
        		{
        			dialog.cancel();
        		}
        	});
    	alertDialogBuilder.setNegativeButton("Yes",
            new DialogInterface.OnClickListener()
    		{
            	public void onClick(DialogInterface dialog, int id1)
            	{
            		db.deleteWithId(Integer.parseInt(id));
                    finish();
            	}  
    		});
            alertDialogBuilder.create().show();
	}
        
	public void editData(View view)
	{
		Intent intent = getIntent();
        intent.setClass(this,EditActivity.class);
        startActivityForResult(intent,EDIT_ENTRY_REQUEST_CODE);
    }
        
	protected void onActivityResult(int requestCode,int responseCode,Intent intent)
	{
		if(responseCode == RESULT_OK)
		{                               
            lat.setText(intent.getStringExtra("latitude"));
            lon.setText(intent.getStringExtra("longitude"));
            alt.setText(intent.getStringExtra("altitude"));
            spe.setText(intent.getStringExtra("speed"));
                
            Toast.makeText(getApplicationContext(), "The Entry is edited successfully",Toast.LENGTH_SHORT).show();
		}
                
		else if (responseCode == RESULT_FAILED)
			Toast.makeText(getApplicationContext(), "There was a problem editing the entry",Toast.LENGTH_SHORT).show();
	}    
}